<?php
declare (strict_types=1);

namespace app\middleware;

class CheckToken
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure $next
     * @return \think\response\Json
     */
    public function handle($request, \Closure $next)
    {


        if (strrpos($request->url(), "/index.php/wechat/wxlogin") === 0) {
            // dump("CheckToken 白名单路径 直接通过");
            return $next($request);
        } else if ($request->url() === "/index.php/wechat/index") {
            return $next($request);
        }
        else if ($request->url() === "/index.php/WxPay/paySuccess") {
            return $next($request);
        }
        else if (strrpos($request->url(), "/index.php/wechat/sendMsg") === 0) {
            return $next($request);
        } else if ($request->url() === "/index.php/wechat/wxRigistByCode") {
            return $next($request);
        } else {

            $headers = $request->header();
            if (array_key_exists("authorization", $headers)) {
                if ($headers['authorization'] == "test") {
                    // jnCYirleo5IDEOevvGjgR2s
                    $request->openid = "ofBd-jnCYirleo5IDEOevvGjgR2s";
                    return $next($request);
                }
                $request->openid = $headers['authorization'];
                return $next($request);
                // $checkRes = checkToken($headers["authorization"]);
                // if ($checkRes['code'] != 1) {
                //     return json($checkRes);
                // } else {
                //
                //     $request->openid = $checkRes["data"]->openid;
                //     return $next($request);
                // }


            } else {
                $data = ['code' => -1, 'msg' => '未携带token'];
                return json($data);
            }


        }

    }
}
